﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OrderManage
{
    public class OrderService
    {
        private List<Order> orders;
        public OrderService()
        {
            orders = new List<Order>();
        }
        public List<Order> GetOrders()
        {
            return orders;
        }
        public void addOrder(Order order)
        {
            if (isUnique(order))
            {
                orders.Add(order);
            }
            else
            {
                throw new MyException("The order has existed.Please modify it.\n"+findID(order.orderId));
            }
        }
        public bool isUnique(Order o)
        {
            foreach(Order item in orders)
            {
                if (o.orderId.Equals(item.orderId))
                {
                    return false;
                }
            }
            return true;
        }
        public void deleteOrder(string id)
        {
            Order order = findID(id);
            if (order!=null)
            {
                orders.Remove(order);
                return;
            }
            throw new MyException("Not find the order.");
        }
        public void modifyCustomer(string id,string customerName) {
            Order o = findID(id);
            if (o != null)
            {
                o.customer = new Customer(customerName);
                return;
            }
            throw new MyException("Not find the order.");
        }
        public void modifyOrderDetail(string id,OrderDetails details)
        {
            Order o = findID(id);
            if (o != null)
            {
                o.orderDetails=details;
                return;
            }
            throw new MyException("Not find the order.");
        }
        public Order findID(string id)
        {
            var query = orders.Where(o=>o.orderId.Equals(id));
            if (query.Count()==0) { return null; }
            return query.ToList()[0];
        }
        public List<Order> findCustomer(string name)
        {
            var query = orders.Where(o=>o.customer.name==name).OrderBy(o=>o.GetMoney());
            return query.ToList();
        }
        public List<Order> findMoney(double money)
        {
            var query = orders.Where(o => o.GetMoney() == money).OrderBy(o => o.orderId);
            return query.ToList();
        }
        public List<Order> findGoods(string name)
        {
            var query = orders.Where(o =>
            {
                foreach (Goods g in o.orderDetails.goods)
                {
                    if (g.name.Equals(name)) { return true; };
                }
                return false;
            }).OrderBy(o=>o.GetMoney());
            return query.ToList();
        }
        public void sort()
        {
            orders.Sort();
        }
        public void sortWithMoney()
        {
            orders.Sort((o1,o2)=> o1.GetMoney().CompareTo(o2.GetMoney()));
        }
        public void sortWithGoodsNumber()
        {
            orders.Sort((o1,o2)=>o1.orderDetails.count-o2.orderDetails.count);
        }
        public void sortWithCustomer()
        {
            orders.Sort((o1, o2) => o1.customer.name.CompareTo(o2.customer.name));
        }
    }
}
